<?php

include "../controller/ConectBanco.php";
header("Content-Type: application/json");

$idUsuario = $_POST["idUsuario"];
//$idUsuario = 6;
$jsonData ='';

$devices  = mysql_query("select * from ponto where idDevice in (select distinct idDevice from ponto NATURAL LEFT JOIN device where idUsuario =$idUsuario)" ) or die (mysql_error());

$jsonData .='{"ponto":[';
while($pt = mysql_fetch_array($devices)){
	$jsonData .= '{"id":"'.$pt["idDevice"].'"
						  ,"nome":"'.$pt["nomePonto"].'"
			              ,"info":"'.$pt["info"].'"
			              ,"icon":"'.$pt["icone"].'"
			              ,"posicoes":[';
	
		$posicoes = mysql_query(" select distinct  latitude, longitude,velocidade,datetime,last
								from package
								where iddevice = ".$pt["idDevice"]."
										and typeData = 12
								        and latitude <> '-0.000000'
								        and longitude <> '-0.000000'
								        and date(realdatetime) = (select date(realdatetime)
																	from package
																   where last = 'true'
			                                                         and typeData in (12)
			                                                         and idDevice = ".$pt["idDevice"]." )")or die (mysql_error());
	

	while($row = mysql_fetch_array($posicoes)){

		$jsonData .= '{"latitude"   :"'.$row["latitude"].'",
                       "longitude"  :"'.$row["longitude"].'",
                       "velocidade" :"'.$row["velocidade"].'",
					   "dateTime"   :"'.date("H:i:s d-m-Y",strtotime($row["datetime"])).'",
					   "last"       :"'.$row["last"].'"},';

	}
	$jsonData = chop($jsonData,",");
	$jsonData .=']},';
}

$jsonData = chop($jsonData,",");
$jsonData .= ']}';

echo $jsonData;

?>